<?php
class User extends AppModel {

	var $name = 'User';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array(
			'Comment' =>
				array('className' => 'Comment',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

			'Post' =>
				array('className' => 'Post',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

			'Vote' =>
				array('className' => 'Vote',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

        'Bookmark' =>
				array('className' => 'Bookmark',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

	);


    var $validate = array(
      'login' => '/^[a-z][a-z0-9]{2,19}$/i',
	  //'login' => '^[a-z][a-z0-9\-]{1,19}(?:[^\-])$/i',
      'password' => '/^[a-z0-9]{6,30}$/i',
      'email' => VALID_EMAIL,
			'threshold' => '/^(\-*\d+)$/',
   );


   function beforeValidate() {
      return true;
	}

    function beforeSave() {
    	 	vendor("filter");
      if(isset($this->data['User']['name'])) $this->data['User']['name'] = strip_tags(trim($this->data['User']['name']));
      if(isset($this->data['User']['site']) && strlen($this->data['User']['site'])>0) {
				$this->data['User']['site'] = strip_tags(trim($this->data['User']['site']));
        if(stristr($this->data['User']['site'], 'http://') === false)
					$this->data['User']['site']="http://".$this->data['User']['site'];

			}
			if(isset($this->data['User']['about'])) $this->data['User']['about'] = filtertext($this->data['User']['about']);

	    if(isset($this->data['User']['password']))
	    	$this->data['User']['password'] = md5($this->data['User']['password']);
		if(!empty($this->data['User']['im']))
			$this->data['User']['im'] = $this->data['User']['ims'] ."/". $this->data['User']['im'];
	    return true;
	}

	function setPassword($id,$newpassword) {
        if ($id)
      	{
          $this->id = $id;
          $this->saveField('password', md5($newpassword));
      	}
	}

    function postcount($uid,$dir) {
		$this->id = $uid;
        if ($dir)
          	$this->saveField('postcount', ($this->field('postcount')+1));
		else
			$this->saveField('postcount', ($this->field('postcount')-1));
	}

    function commentcount($uid,$dir) {
		$this->id = $uid;
        if ($dir)
          	$this->saveField('comcount', ($this->field('comcount')+1));
		else
			$this->saveField('comcount', ($this->field('comcount')-1));
	}

    function setvote($uid,$v) {
        $this->id = $uid;
       	$this->saveField('vote', ($this->field('vote')+$v));
	}

	function lastseen($uid) {
    $this->id = $uid;
   	$this->saveField('lastseen', date('Y-m-d H:i:s'));
	}

	function userdata($uid) {
		$fields=array('User.id','User.login','User.password','User.name','User.lastseen','User.vote','User.type','User.threshold','User.thread','User.avatar','User.tz');
		$this->recursive = 0;
		$u=$this->read($fields,$uid);
		return $u['User'];
	}

}
?>